package com.lcredu.lcrliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcredu.common.util.PageUtils;
import com.lcredu.common.util.R;
import com.lcredu.lcrliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.lcredu.lcrliving.commodity.entity.AttrEntity;
import com.lcredu.lcrliving.commodity.service.AttrAttrgroupRelationService;
import com.lcredu.lcrliving.commodity.service.AttrService;
import com.lcredu.lcrliving.commodity.service.CategoryService;
import com.lcredu.lcrliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcredu.lcrliving.commodity.entity.AttrgroupEntity;
import com.lcredu.lcrliving.commodity.service.AttrgroupService;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author lcr
 * @email lcr@gmail.com
 * @date 2023-10-07 15:32:36
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;

    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 1. 根据业务需求,增加具有分类条件的方法
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrgroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id) {
        AttrgroupEntity attrgroup = attrgroupService.getById(id);


        //获取到该属性组对应的categoryId
        Long categoryId = attrgroup.getCategoryId();

        //获取到categoryId对应的cascadedCategoryId形式[1,21,301]
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);

        //把cascadedCategoryId设置到attrgroup对象的cascadedCategoryId属性
        attrgroup.setCascadedCategoryId(cascadedCategoryId);

        System.out.println("商品号为" + attrgroup.getCascadedCategoryId().toString());

        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup) {
        attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup) {
        attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids) {
        attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //装配AttrService
    @Resource
    private AttrService attrService;

    //根据属性组id,返回该属性组关联的基本属性
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntity = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntity);
    }

    /**
     * 删除属性组 - 属性
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities) {
        System.out.println("1");
        attrService.deleteRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }

    /**
     * 根据attrgroupId返回可以关联的基本属性
     */
    @RequestMapping("/{attrgroupId}/attr/allowrelation")
    public R attrAllowRelation(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getAllowRelationAttr(params, attrgroupId);

        return R.ok().put("page", page);
    }

    //装配AttrAttr
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 批量添加属性组和属性(基本属性)的关联关系
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities) {
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }

    /**
     * 获取某个分类的所有属性组及其关联的基本属性
     */
    @RequestMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catalogId")Long categoryId){
        List<AttrGroupWithAttrsVo> AttrGroupWithAttrsVos =
                attrgroupService.getAttrGroupWithAttrsByCategoryId(categoryId);
        return R.ok().put("data",AttrGroupWithAttrsVos);
    }
}
